﻿using AutoMapper;
using JunionRobotTaskManager.Dtos;
using JunionRobotTaskManager.Models;
using Newtonsoft.Json;

namespace JunionRobotTaskManager.Utility
{
    public  class CustomAutoMapperProfile: Profile
    {
        public CustomAutoMapperProfile()
        {
            base.CreateMap<RobotDto, Robot>().ForMember(dest => dest.Payloads, source => source.MapFrom(src => src.Payloads.Split(',', StringSplitOptions.None).ToArray()));
            base.CreateMap<Robot, RobotDto>().ForMember(dest => dest.Payloads, source => source.MapFrom(src => string.Join(',', src.Payloads)));


            base.CreateMap<Job, JobDto>().ForMember(dest => dest.Targets, source => source.MapFrom(src => JsonConvert.SerializeObject(src.Targets)));

            //SystemInfo->SystemInfoDto
            base.CreateMap<SystemInfo, SystemInfoDto>().ForMember(dest => dest.AvailableStatus, source => source.MapFrom(src => src.AvailableStatus.ToArray()))   //string.Join(',', src.AvailableStatus))
                .ForMember(dest => dest.CalibrationEnableStartTime, source => source.MapFrom(src => src.CalibrationSettings.CalibrationEnableStartTime.ToString()))
                .ForMember(dest => dest.CalibrationEnableEndTime, source => source.MapFrom(src => src.CalibrationSettings.CalibrationEnableEndTime.ToString()))
                .ForMember(dest => dest.EnableCalibration, source => source.MapFrom(src => src.CalibrationSettings.EnableCalibration))
                .ForMember(dest => dest.TimeNode, source => source.MapFrom(src => src.CalibrationSettings.TimeNode))
                .ForMember(dest => dest.CalibrationSpan, source => source.MapFrom(src => src.CalibrationSettings.CalibrationSpan));

            //SystemInfoDto->SystemInfo
            base.CreateMap<SystemInfoDto, SystemInfo>().ForMember(dest => dest.AvailableStatus, source => source.MapFrom(src => src.AvailableStatus.ToList()))  //Split(',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)
                .ForPath(dest => dest.CalibrationSettings.CalibrationEnableStartTime, source => source.MapFrom(src => TimeSpan.Parse(src.CalibrationEnableStartTime.ToString())))
                .ForPath(dest => dest.CalibrationSettings.CalibrationEnableEndTime, source => source.MapFrom(src => TimeSpan.Parse(src.CalibrationEnableEndTime.ToString())))
                .ForPath(dest => dest.CalibrationSettings.EnableCalibration, source => source.MapFrom(src => src.EnableCalibration))
                .ForPath(dest => dest.CalibrationSettings.TimeNode, source => source.MapFrom(src => src.TimeNode))
                .ForPath(dest => dest.CalibrationSettings.CalibrationSpan, source => source.MapFrom(src => src.CalibrationSpan));
        }
    }
}
